package com.work.util;

import com.sun.mail.util.MailSSLSocketFactory;
import org.apache.commons.io.FileUtils;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeUtility;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.net.URLDecoder;
import java.security.GeneralSecurityException;
import java.util.List;
import java.util.Properties;

/**
 * @Description 邮件工具类
 * @Author WangWenpeng
 * @Date 20:38 2020/1/31
 * @Param
 */
public class EmailUtil {


    public EmailUtil() {

    }

    /**
     * @Description 发邮件方法
     * @Author WangWenpeng
     * @Date 20:38 2020/1/31
     * @Param [toUserEmail, subject, comment]
     */
    public void sendEmail(String subject, String comment) throws Exception {
        String emailSender = "";
        String authCode = "";
        List<String> toEmailList = null;

        //发件箱和授权码 用公司的qq邮箱
        Properties senderP = new Properties();
        ClassLoader classLoader = getClass().getClassLoader();
        try {
            senderP.load(classLoader.getResource("emailSender.properties").openStream());
            emailSender = (String) senderP.get("sender.senderEmail");
            authCode = (String) senderP.get("sender.authCode");
        } catch (IOException e) {
            e.printStackTrace();
        }

        //收件箱的列表 读收件的配置文件，不同于发件的，收件的是list
        URL toEmailFile = classLoader.getResource("emailReceiver.properties");
        try {
            toEmailList = FileUtils.readLines(new File(URLDecoder.decode(toEmailFile.getPath(), "utf-8")));
        } catch (IOException e) {
            e.printStackTrace();
        }

        //给收件人发送邮件信息
        for (String receiverEmail : toEmailList) {
            Properties properties = new Properties();
            //QQ邮件服务器
            properties.setProperty("mail.host", "smtp.qq.com");
            properties.setProperty("mail.transport.protocol", "smtp");
            properties.setProperty("mail.smtp.auth", "true");

            //QQ存在一个特性设置SSL加密
            MailSSLSocketFactory sf = new MailSSLSocketFactory();
            sf.setTrustAllHosts(true);
            properties.put("mail.smtp.ssl.enable", "true");
            properties.put("mail.smtp.ssl.socketFactory", sf);

            //创建一个session对象
            Session session = Session.getDefaultInstance(properties, new Authenticator() {
                @Override
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication("qq邮箱", "QQ邮箱16位授权码");
                }
            });
            //开启debug模式
            session.setDebug(true);
            //获取连接对象
            Transport transport = session.getTransport();
            //连接服务器
            transport.connect("smtp.qq.com", emailSender, authCode);
            //创建邮件对象
            MimeMessage mimeMessage = new MimeMessage(session);
            //邮件发送人
            mimeMessage.setFrom(new InternetAddress(MimeUtility.encodeWord("疫情动态") + " <" + emailSender + ">"));
            //邮件接收人
            mimeMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(receiverEmail));
            //邮件标题
            mimeMessage.setSubject(subject);
            //邮件内容
            mimeMessage.setContent(comment, "text/html;charset=UTF-8");
            //发送邮件
            transport.sendMessage(mimeMessage, mimeMessage.getAllRecipients());
            //关闭连接
            transport.close();
        }


    }
}
